<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    class EventEmitter {
      constructor() {
        this.subs = {}
        // { give: [handler1, handler2], 'flow-event': ?,  'bill-event': ?}
      }
      // 触发
      $emit(eventType) {
        this.subs[eventType].forEach(handler => {
          handler()
        })
      }
      // 注册
      $on(eventType, handler) {
        this.subs[eventType] = this.subs[eventType] || []
        this.subs[eventType].push(handler)
      }
    }
    const sub = new EventEmitter()

    // 组件1
    sub.$on('give', () => {
      console.log('object1');
    })
    // 组件2
    sub.$on('give', () => {
      console.log('object2');
    })
    sub.$on('bill-event', () => {
      console.log('bill-event');
    })
    // 发布
    sub.$emit('give')
    sub.$emit('bill-event')
  </script>
</body>

</html>